package demo1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class RegisterFrame extends JFrame {
    private static final String USER_FILE = "users.txt";

    private JTextField usernameField;
    private JPasswordField passwordField;

    public RegisterFrame() {
        setTitle("注册");
        setSize(316, 190);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 创建一个JLayeredPane
        JLayeredPane layeredPane = new JLayeredPane();
        setContentPane(layeredPane);

        // 添加背景图层
        JLabel background = new JLabel(new ImageIcon("images/2.jpg"));
        background.setBounds(0, 0, 300, 150); // 设置背景图的位置和大小
        layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);

        // 创建一个面板来放置用户名、密码和注册按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(false); // 设置面板透明
        panel.setBounds(0, 0, 300, 150); // 设置面板位置和大小
        layeredPane.add(panel, JLayeredPane.PALETTE_LAYER);

        JPanel contentPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        contentPanel.setOpaque(false); // 设置内容面板透明
        panel.add(contentPanel, BorderLayout.CENTER);

        JLabel usernameLabel = new JLabel("用户名:");
        contentPanel.add(usernameLabel);
        usernameField = new JTextField(20);
        contentPanel.add(usernameField);

        JLabel passwordLabel = new JLabel("密码:");
        contentPanel.add(passwordLabel);
        passwordField = new JPasswordField(20);
        contentPanel.add(passwordField);

        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                if (registerUser(username, password)) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                    dispose(); // 关闭注册窗口
                } else {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "注册失败，用户名已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        panel.add(registerButton, BorderLayout.SOUTH);

        setLocationRelativeTo(null); // 居中显示
        setVisible(true);
    }

    private boolean registerUser(String username, String password) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(USER_FILE, true))) {
            // 检查用户名是否已存在
            if (checkUserExists(username)) {
                return false;
            }

            // 写入新用户信息
            writer.write(username + "," + password);
            writer.newLine();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private boolean checkUserExists(String username) {
        try (BufferedReader reader = new BufferedReader(new FileReader(USER_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length > 0 && parts[0].equals(username)) {
                    return true; // 找到相同的用户名
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false; // 没有找到相同的用户名
    }
}
